home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
smaltalk
/
manchest.lha
/
MANCHESTER
/
manchester
/
4.1
/
Games-Dice.st
< prev
next >
Wrap
Text File
|
1993-07-24
|
5KB
|
201 lines
" NAME Games-Dice
AUTHOR tph@cs.man.ac.uk <Trevor Hopkins>
CONTRIBUTOR mario@cs.man.ac.uk
FUNCTION alternative decision-making
ST-VERSIONS 4.1
PREREQUISITES
CONFLICTS
DISTRIBUTION world
VERSION 1.2
DATE Sep 1992
SUMMARY Games-Dice
is a simple alternative to conventional decision-making!!
(Actually, a simple illustration of MVC.-ed)
Written by Trevor Hopkins, display by Bernard Horan.
"!
ControllerWithMenu subclass: #DiceController
instanceVariableNames: ''
classVariableNames: 'DiceMenu '
poolDictionaries: ''
category: 'Games-Dice'!
DiceController comment:
'I represent the controller for a DiceView.'!
!DiceController methodsFor: 'menu messages'!
menu
^DiceMenu!
roll
model roll! !
!DiceController methodsFor: 'control defaults'!
controlActivity
self sensor keyboardPressed ifTrue:[self readKeyboard].
^super controlActivity!
readKeyboard
"Roll the die if an 'r' or 'R' is typed.
Set it to a particular value if a digit is typed."
| char digit |
char := self sensor keyboardEvent keyCharacter.
char asLowercase = $r ifTrue: [self roll].
(char isDigit and: [digit := char digitValue. digit between: 1 and: 6])
ifTrue: [model value: digit]! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
DiceController class
instanceVariableNames: ''!
!DiceController class methodsFor: 'class initialization'!
initialize
"DiceController initialize"
"Initialize the menu."
super initialize.
DiceMenu := PopUpMenu labels: 'roll' values: #(roll)! !
Model subclass: #Dice
instanceVariableNames: 'value randomGenerator '
classVariableNames: ''
poolDictionaries: ''
category: 'Games-Dice'!
Dice comment:
'I represent a single Dice (strictly, a DIE). I respond to the roll message to generate a new random number.'!
!Dice methodsFor: 'accessing'!
value
"Answer with the value represented by the receiver."
^value!
value: aValue
"This is how to load the die!!"
value := aValue.
self changed! !
!Dice methodsFor: 'initialize-release'!
initialize
"Initialize the random number generator, and set up the initial
state of the receiver."
randomGenerator := Random new.
self rollDice! !
!Dice methodsFor: 'modifying'!
roll
"Update the dice value randomly several times, to give the impression
of a real roll."
6 timesRepeat: [
self rollDice.
self changed]!
rollDice
"Update the dice value randomly."
value := (randomGenerator next * 6 + 1) truncated! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
Dice class
instanceVariableNames: ''!
!Dice class methodsFor: 'instance creation'!
new
"Answer with a new instance of the receiver."
^super new initialize! !
View subclass: #DiceView
instanceVariableNames: 'spot spotWidth spotHeight '
classVariableNames: ''
poolDictionaries: ''
category: 'Games-Dice'!
DiceView comment:
'I know how to display a Dice in a viewable manner.'!
!DiceView methodsFor: 'controller access'!
defaultControllerClass
^DiceController! !
!DiceView methodsFor: 'displaying'!
displayOn: aGraphicsContext
| box cent offset |
box := self bounds.
cent := box center.
offset := cent - (spotWidth // 2 @ (spotHeight // 2)).
(#(1 3 5 ) includes: model value)
ifTrue: [aGraphicsContext displayImage: spot at: offset].
(#(2 3 4 5 6 ) includes: model value)
ifTrue:
[aGraphicsContext displayImage: spot at: offset + (cent - box topLeft // 2).
aGraphicsContext displayImage: spot at: offset + (cent - box bottomRight // 2)].
(#(4 5 6 ) includes: model value)
ifTrue:
[aGraphicsContext displayImage: spot at: offset + (cent - box topRight // 2).
aGraphicsContext displayImage: spot at: offset + (cent - box bottomLeft // 2)].
6 = model value
ifTrue:
[aGraphicsContext displayImage: spot at: offset + (cent - box leftCenter // 2).
aGraphicsContext displayImage: spot at: offset + (cent - box rightCenter // 2)]! !
!DiceView methodsFor: 'updating'!
update: aSymbol
"This is a frig to make sure all updates are visible."
self invalidateRectangle: self bounds repairNow: true! !
!DiceView methodsFor: 'bounds accessing'!
bounds: newBounds
"Resizing the dice changes the sizes and positions of the spots."
|pixmap aGC |
super bounds: newBounds.
spotWidth := newBounds width // 6.
spotHeight := newBounds height // 6.
pixmap := Pixmap extent: spotWidth@spotHeight.
aGC := pixmap graphicsContext.
aGC displayWedgeBoundedBy: aGC clippingBounds startAngle: 0 sweepAngle: 360.
aGC paint: ColorValue gray.
aGC displayWedgeBoundedBy: (aGC clippingBounds scaledBy: 0.95) startAngle: 0 sweepAngle: 360.
spot := pixmap asImage! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
DiceView class
instanceVariableNames: ''!
!DiceView class methodsFor: 'instance creation'!
open
"Create a new instance on the receiver with a new model"
"DiceView open."
self openOn: Dice new!
openOn: aDice
"DiceView openOn: Dice new"
| window diceView |
window := ScheduledWindow new model: aDice.
window label: aDice class name.
window minimumSize: 100@100.
diceView := self new model: aDice.
window component: (BoundedWrapper on: diceView).
window open! !
DiceController initialize!